package com.epic;

import java.util.ArrayList;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.text.Html;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.TextView;

import com.eastandroid.zoom.GestureBase;
import com.eastandroid.zoom.GestureZoom;
import com.eastandroid.zoom.ZoomState;
import com.eastandroid.zoom.ZoomState.MinSize;
import com.eastandroid.zoom.ZoomState.Position;
import com.eastandroid.zoom.ZoomState.UserTouch;
import com.eastandroid.zoom.ZoomState.ZoomStyle;
import com.eastandroid.zoom.ZoomStateView;
import com.eastandroid.zoom.ZoomView;
import com.epic.adapter.FragmentGalleryAdapter;
import com.epic.item.MediaItem;
import com.epic.network.NetworkEpic;
import com.epic.network.OnReceiverDataListener;
import com.epic.pager.OnModePagerListener;
import com.epic.pager.OnModeZoomListener;
import com.loopj.android.image.ZooViewPager;

public class ActivityViewerImage extends FragmentActivity implements
		OnModeZoomListener, OnModePagerListener, OnReceiverDataListener,
		OnPageChangeListener {
	private ZooViewPager viewPager;
	private FragmentGalleryAdapter adapter;

	private static enum MODE {
		ZOOM, DRAW
	};

	private MODE mode = MODE.ZOOM;
	private ZoomView mZoomView;
	private ZoomStateView mZoomStateView;
	private ZoomState mZoomState;
	private GestureZoom mZoomGesture;
	private View layoutZoom;
	private TextView counterView;
	private View bottomView;
	private TextView title;
	private boolean isFirstTime;
	private TextView notification;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_viewer);
		isFirstTime = true;
		viewPager = (ZooViewPager) findViewById(R.id.pager);
		layoutZoom = findViewById(R.id.zoomLayout);
		mZoomView = (ZoomView) findViewById(R.id.zoomview);
		notification = (TextView) findViewById(R.id.notificationError);
		mZoomStateView = (ZoomStateView) findViewById(R.id.zoomstateview);
		counterView = (TextView) findViewById(R.id.counterImage);
		bottomView = findViewById(R.id.layout_bottom);
		
		title = (TextView) findViewById(R.id.title);
		initZoomView();
		adapter = new FragmentGalleryAdapter(getSupportFragmentManager(),
				new ArrayList<MediaItem>());
		viewPager.setOnPageChangeListener(this);
		viewPager.setPageMargin(20);
		viewPager.setAdapter(adapter);
		NetworkEpic.getInstance().requestMedia(this);
	}

	private void initZoomView() {
		mZoomState = new ZoomState();
		mZoomState.setOnModePagerListener(this);
		mZoomState.deleteObservers();
		mZoomState.setPosition(Position.INSIDE);
		mZoomState.setZoomStyle(ZoomStyle.RECT);
		mZoomState.setMinSize(MinSize.WIDTH);
		mZoomState.setUserTouch(UserTouch.TOUCHAFTER);
		mZoomState.notifyObservers();

		mZoomGesture = new GestureZoom();
		GestureBase.setZoomState(mZoomState);
		mZoomView.setZoomState(mZoomState);
		mZoomView.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				switch (mode) {
				case ZOOM:
					mZoomGesture.onTouchEvent(event);
					break;
				default:
					break;
				}
				return true;
			}
		});
		mZoomState.addObserver(mZoomView);
		mZoomStateView.setZoomState(mZoomState);
		mZoomGesture.setOnEventDebugListener(mZoomStateView);
	}

	@Override
	public void onModeZoomListener(String url) {
		viewPager.setPagingEnabled(false);
		counterView.setVisibility(View.GONE);
		bottomView.setVisibility(View.GONE);
		layoutZoom.setVisibility(View.VISIBLE);
		viewPager.setVisibility(View.GONE);
	}

	@Override
	public void onModePagerListener() {
		viewPager.setPagingEnabled(true);
		mZoomView.setImageBitmap(null);
		viewPager.setVisibility(View.VISIBLE);
		layoutZoom.setVisibility(View.GONE);
		
		counterView.setVisibility(View.VISIBLE);
		bottomView.setVisibility(View.VISIBLE);
	}

	@Override
	public void onNewDataReceiver(ArrayList<MediaItem> data) {
		adapter.addData(data);
		if (isFirstTime) {
			isFirstTime = false;
			onPageSelected(0);
		}
	}

	@Override
	public void onPageScrollStateChanged(int arg0) {

	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {

	}

	@Override
	public void onPageSelected(int position) {
		String caption = adapter.getCaption(position);
		String url = adapter.getUrlMedia(position);
		mZoomView.setImageUrl(url);
		caption = Html.fromHtml(caption).toString();
		title.setText(caption);
		if (position == adapter.getCount() - 1) {
			NetworkEpic.getInstance().requestMedia(this);
		}
	}

	@Override
	public void onError() {
		notification.setVisibility(View.VISIBLE);
	}
}
